﻿<template>
  <el-dialog
    v-drag-dialog
    :title="dlgTitle"
    :visible.sync="dialogFormVisible"
    ref="dlg"
    width="800px"
    :close-on-click-modal="false"
  >
    <el-form
      :rules="rules"
      ref="dataForm"
      :model="temp"
      label-position="right"
      label-width="130px"
      size="small"
      style="width: 80%; margin-left:6px;"
    >
      <el-form-item label="课程序号：" prop="CourseDataID" v-if="temp.CourseDataID>0">
        <el-input placeholder="课程序号" v-model="temp.CourseDataID" :disabled="temp.CourseDataID>0"></el-input>
      </el-form-item>
      <el-form-item label="课程号：" prop="CourseNo">
        <el-input placeholder="课程号" v-model="temp.CourseNo" ></el-input>
      </el-form-item>
      <el-form-item label="课程名：" prop="CourseName">
        <el-input placeholder="课程名" v-model="temp.CourseName" ></el-input>
      </el-form-item>
      <el-form-item label="课程学分：" prop="CourseCredit">
        <el-input placeholder="课程学分" v-model="temp.CourseCredit" ></el-input>
      </el-form-item>
      <el-form-item label="开设学期：" prop="CourseTerm">
        <el-select class="filter-item" v-model="temp.CourseTerm" width="100%">
          <el-option
            v-for="item in courseOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程介绍：" prop="CourseInfo">
        <el-input placeholder="课程介绍" v-model="temp.CourseInfo" ></el-input>
      </el-form-item>
    
      
      <el-form-item label="课程类型：" prop="CourseType">
        <el-radio-group v-model="temp.CourseType">
          <el-radio 
            v-for="item in CourseTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-radio>
      </el-radio-group>
      </el-form-item>
      
      <el-form-item label="注册日期：" prop="RegDateTime">
            <el-date-picker
          v-model="temp.RegDateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="Cancel">取消</el-button>
      <el-button type="primary" @click="Save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>

import { saveCourseInfo, getCourseInfo } from '@/api/course';
export default {
  components: {
    
  },
  data() {
    let checkMobile = (rule, value, callback) => {
      if (!/^1[3-9]\d{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'));
      } else callback();
    };
    return {
      dlgTitle: '',
      dialogFormVisible: false,
      temp: {
        CourseDataID: 0,
      },
      rules: {
        CourseNo: [
          { required: true, message: '必填字段1', trigger: 'blur' }
        ],
        CourseName: [
          { required: true, message: '必填字段2', trigger: 'blur' }
        ],
        CourseCredit: [
          { required: true, message: '必填字段3', trigger: 'blur' }
        ],
        CourseTerm: [
          { required: true, message: '必填字段4', trigger: 'blur' }
        ]
       
      },
      courseOptions: [
        {
          value: "1",
          label: "1"
        },
        {
          value: "2",
          label: "2"
        },
        {
          value: "3",
          label: "3"
        },
        {
          value: "4",
          label: "4"
        },
        {
          value: "5",
          label: "5"
        },
        {
          value: "6",
          label: "6"
        },
        {
          value: "7",
          label: "7"
        },
        {
          value: "8",
          label: "8"
        },
       

      ],
        CourseTypeOptions: [
        {
          value: "公共课",
          label: "公共课"
        },
        {
          value: "基础课",
          label: "基础课"
        },
        {
          value: "专业课",
          label: "专业课"
        }
      ],
  
      
    };
  },
  methods: {
    AddDialog(data) {
      let row = data || null;
      this.initInfo();
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
      this.dlgTitle = '添加课程';
    },
    EditDialog(id) {
      this.initInfo();
      this.dialogFormVisible = true;
      this.temp.CourseDataID = id;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
      this.dlgTitle = '修改课程';
      this.getRecordInfo(this.temp.CourseDataID);
    },
    initInfo() {
      this.temp.CourseDataID = 0;
      this.temp = {};
    },
    getRecordInfo(id) {
      getCourseInfo(id).then(rsp => {
        if (rsp.code === 0) {
          let record = rsp.data;
          this.temp = record;
        } else {
          this.dialogFormVisible = false;
          this.$message.error(rsp.msg);
        }
      });
    },
    Save() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm('确认保存？', '提示', {
            type: 'warning',
            callback: action => {
              if (action == 'confirm') {
                saveCourseInfo(this.temp).then(rsp => {
                  if (rsp.code === 0) {
                    this.dialogFormVisible = false;
                    this.$message.success(this.$refs.dlg.title + '成功!');
                    this.$emit('onRefresh');
                  } else {
                    this.$message.error(rsp.msg);
                  }
                });
              }
            }
          });
        }
      });
    },
    Cancel() {
      this.dialogFormVisible = false;
    }
  }
};
</script>
